package com.it.tencent.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.it.tencent.usercenter.constant.UserConstant;
import com.it.tencent.usercenter.model.domain.User;
import com.it.tencent.usercenter.model.request.UserRequest;
import com.it.tencent.usercenter.service.UserService;
import com.it.tencent.usercenter.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController implements UserConstant{


    @Autowired
    UserService userService;
    User user = new User();

    @PostMapping("/register")
    public Long register(@RequestBody UserRequest userRequest) {
        if(userRequest==null){
            return null;
        }
        String password = userRequest.getPassword();
        String account = userRequest.getAccount();
        String checkPassword = userRequest.getCheckPassword();
        if(StringUtils.isBlank(password)||StringUtils.isBlank(account)||StringUtils.isBlank(checkPassword)){
            return null;
        }
        user.setAccount(account);
        user.setPassword(password);
        user.setCheckPassword(checkPassword);
       return  userService.Register(user);
    }

    @PostMapping("/login")
    public User login(@RequestBody UserRequest userRequest, HttpServletRequest request) {
        if(userRequest==null){
            return null;
        }
        String password = userRequest.getPassword();
        String account = userRequest.getAccount();
        if(StringUtils.isBlank(password)||StringUtils.isBlank(account)){
            return null;
        }
        user.setAccount(account);
        user.setPassword(password);
        return  userService.Login(user,request);
    }

    @GetMapping("/search")
    public List<User> searchUser( String username,HttpServletRequest request){
        if(!isAdmin(request)){
            return new ArrayList<>();
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(username)){
            queryWrapper.like("name", username);
        }
        List<User> userList = userService.list(queryWrapper);
        userList.stream().map(user ->{
            User safetyUser = userService.safetyUser(user);
            return safetyUser;
        }).collect(Collectors.toList());
        return userList;
    }

    @PostMapping("/delete")
    public boolean deleteUser(@RequestBody long id,HttpServletRequest request) {
        if(!isAdmin(request)){
            return false;
        }
        if(id<=0){
        return false;
        }
        return userService.removeById(id);
    }

    public boolean isAdmin(HttpServletRequest request){
        Object rUser = request.getSession().getAttribute(LOGIN_STATE);
        user = (User)rUser;
        if(user==null || user.getUserRole()==USER_ID){
            return false;
        }
        return true;
    }
}
